package com.be.beadmin.material.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.material.dto.input.MaterialCodeInfoDto;
import com.be.beadmin.material.dto.output.MaterialCodeInfoEx;
import com.be.beadmin.material.service.MaterialCodeInfoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zmy
* @date 2023-06-11
**/
@RestController
public class MaterialCodeInfoController {

    @Autowired
    private MaterialCodeInfoService materialCodeInfoService;


    @PostMapping(value = "/product/getMaterialCodeInfoByPage")
    public ResultModel getMaterialCodeInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(materialCodeInfoService.getMaterialCodeInfoByPage(pageEntity)) ;
    }

    @PostMapping("/product/addMaterialCodeInfo")
    public ResultModel addMaterialCodeInfo(@RequestBody MaterialCodeInfoDto materialCodeInfoDto){
        materialCodeInfoService.addMaterialCodeInfo(materialCodeInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delMaterialCodeInfo")
    public ResultModel delMaterialCodeInfo(@RequestBody MaterialCodeInfoDto materialCodeInfoDto){
        materialCodeInfoService.delMaterialCodeInfo(materialCodeInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updateMaterialCodeInfo")
    public ResultModel updateMaterialCodeInfo(@RequestBody MaterialCodeInfoDto materialCodeInfoDto){
        materialCodeInfoService.updateMaterialCodeInfo(materialCodeInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportMaterialCodeInfo")
    public void exportMaterialCodeInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<MaterialCodeInfoEx> materialCodeInfoPage = materialCodeInfoService.getMaterialCodeInfoByPage(pageEntity);
        List<MaterialCodeInfoEx> materialCodeInfoExes = materialCodeInfoPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("MaterialCodeInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), MaterialCodeInfoEx.class).sheet("sheet1").doWrite(materialCodeInfoExes);
    }
    @PostMapping("/design/addInfo")
    public ResultModel addInfo(@ApiParam(value = "物料code信息文件",required = true) @RequestParam("file") MultipartFile[] multipartFiles) throws IOException {

        materialCodeInfoService.addInfo(multipartFiles);

        return ResultModel.success();
    }

    @ApiOperation("根据物料编码获取物料名称")
    @PostMapping("/product/getMaterialNameByMaterialCode")
    public ResultModel getMaterialNameByMaterialCode(@RequestParam String  materialCode){
        return ResultModel.success(materialCodeInfoService.getMaterialNameByMaterialCode(materialCode));
    }
}